package example

import (
	"bytes"
	"fmt"
	"io"
	"strings"
)

// 多读写器

func Case6() {
	fmt.Println("🎯多读写器")

	// 1. io.MultiReader - 多个读取器串联
	reader1 := strings.NewReader("第一个读取器 ")
	reader2 := strings.NewReader("第二个读取器 ")
	reader3 := strings.NewReader("第三个读取器")

	multiReader := io.MultiReader(reader1, reader2, reader3)

	// 从组合的读取器读取
	combined, _ := io.ReadAll(multiReader)
	fmt.Printf("MultiReader结果: %s\n", string(combined))

	// 2. io.MultiWriter - 同时写入多个目标
	var buf1, buf2 bytes.Buffer
	multiWriter := io.MultiWriter(&buf1, &buf2)

	content := "同时写入多个目标的数据"
	multiWriter.Write([]byte(content))

	fmt.Printf("缓冲区1: %s\n", buf1.String())
	fmt.Printf("缓冲区2: %s\n", buf2.String())

	// 3. io.TeeReader - 读取时同时写入
	source := strings.NewReader("原始数据")
	var teeBuf bytes.Buffer
	teeReader := io.TeeReader(source, &teeBuf)

	// 从TeeReader读取
	teeData, _ := io.ReadAll(teeReader)
	fmt.Printf("从TeeReader读取: %s\n", string(teeData))
	fmt.Printf("Tee写入缓冲: %s\n", teeBuf.String())
}
